<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:insert="~{include :: header('编辑设备指令')}" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-command-edit" th:object="${devDeviceCommand}">
                        <input type="hidden" name="commandId" th:value="${devDeviceCommand.commandId}">
                        <div class="form-group">
                            <label class="col-sm-3 control-label">设备ID：</label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" name="deviceId" id="deviceId" th:value="${devDeviceCommand.deviceId}" readonly>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">指令类型：</label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" name="commandType" id="commandType" th:value="${devDeviceCommand.commandType}" readonly>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">指令内容：</label>
                            <div class="col-sm-8">
                                <textarea class="form-control" name="commandContent" id="commandContent" rows="4" th:text="${devDeviceCommand.commandContent}" th:attr="readonly=${devDeviceCommand.status eq 'SENT' or devDeviceCommand.status eq 'COMPLETED' or devDeviceCommand.status eq 'FAILED' or devDeviceCommand.status eq 'TIMEOUT'}"></textarea>
                                <span class="help-block m-b-none" th:if="${devDeviceCommand.status eq 'SENT' or devDeviceCommand.status eq 'COMPLETED' or devDeviceCommand.status eq 'FAILED' or devDeviceCommand.status eq 'TIMEOUT'}"><i class="fa fa-info-circle"></i> 已发送或已完成的指令内容不可修改</span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">指令状态：</label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" th:value="${#strings.defaultString(#vars.devDeviceCommand.status, '') eq 'WAITING' ? '待发送' : (#vars.devDeviceCommand.status eq 'SENT' ? '已发送' : (#vars.devDeviceCommand.status eq 'COMPLETED' ? '执行成功' : (#vars.devDeviceCommand.status eq 'FAILED' ? '执行失败' : (#vars.devDeviceCommand.status eq 'TIMEOUT' ? '超时' : #vars.devDeviceCommand.status))))}" readonly>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">重试次数：</label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" th:value="${devDeviceCommand.retryCount}" readonly>
                            </div>
                        </div>
                        <div class="form-group" th:if="${devDeviceCommand.sendTime}">
                            <label class="col-sm-3 control-label">下发时间：</label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" th:value="${#dates.format(devDeviceCommand.sendTime, 'yyyy-MM-dd HH:mm:ss')}" readonly>
                            </div>
                        </div>
                        <div class="form-group" th:if="${devDeviceCommand.executeTime}">
                            <label class="col-sm-3 control-label">完成时间：</label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" th:value="${#dates.format(devDeviceCommand.executeTime, 'yyyy-MM-dd HH:mm:ss')}" readonly>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">过期时间：</label>
                            <div class="col-sm-8">
                                <input type="datetime-local" class="form-control" name="expireTime" id="expireTime" th:value="${devDeviceCommand.expireTime != null ? #dates.format(devDeviceCommand.expireTime, 'yyyy-MM-dd''T''HH:mm') : ''}" th:attr="readonly=${devDeviceCommand.status eq 'SENT' or devDeviceCommand.status eq 'COMPLETED' or devDeviceCommand.status eq 'FAILED' or devDeviceCommand.status eq 'TIMEOUT'}">
                            </div>
                        </div>
                        <!-- 不使用自定义按钮，使用模态框自带的确定/关闭按钮 -->
                        <input type="hidden" name="submitType" value="confirm" />
                        <input type="hidden" id="commandStatus" th:value="${devDeviceCommand.status}" />
        </form>
    </div>
<th:block th:insert="~{include :: footer}" />
<script th:inline="javascript">
        var prefix = ctx + "deviceManager/command";
        $("#form-command-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            // 获取指令状态
            var commandStatus = $("#commandStatus").val();
            var disabledStatuses = ['SENT', 'COMPLETED', 'FAILED', 'TIMEOUT'];
            var isDisabled = disabledStatuses.includes(commandStatus);
            
            if (!isDisabled && $.validate.form()) {
                $.operate.save(prefix + "/edit", $('#form-command-edit').serialize());
            }
        }
    </script>
</body>
</html>